package com.snow.useviewdemo.function2.printer;

import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.snow.useviewdemo.R;
import com.snow.useviewdemo.base.BaseActivity;
import com.snow.useviewdemo.util.HandlerHelper;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhaoyingtao
 * Date: 2021/3/19
 * Describe:
 */
public class PrinterStartActivity extends BaseActivity {
    @BindView(R.id.printerTextView)
    PrinterTextView printerTextView;
    @BindView(R.id.btn_start)
    Button btnStart;
    PrinterIntentBean intentBean;
    @BindView(R.id.root_rl)
    RelativeLayout rootRl;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_start_printer;
    }

    @Override
    protected void setViewData(Bundle savedInstanceState) {
        intentBean = (PrinterIntentBean) getIntent().getSerializableExtra("intentBean");
        if (intentBean != null) {
            setTextStyle(printerTextView, intentBean.getTextStyle());
            printerTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, intentBean.getTextSize());
            if (!TextUtils.isEmpty(intentBean.getBgColor())) {
                rootRl.setBackgroundColor(Color.parseColor(intentBean.getBgColor()));
            } else {
                rootRl.setBackgroundResource(R.mipmap.bg_printer_background);
            }
            if (!TextUtils.isEmpty(intentBean.getTextColor())) {
                printerTextView.setTextColor(Color.parseColor(intentBean.getTextColor()));
            } else {
                printerTextView.setTextColor(getResources().getColor(R.color.color_323232));
            }
        } else {
            Toast.makeText(this, "当前数据为空", Toast.LENGTH_SHORT).show();
        }
        initListener();
    }

    private void initListener() {
        btnStart.setOnClickListener(view -> {
            btnStart.setVisibility(View.GONE);
            HandlerHelper.postDelay(() -> {
                startPrint();
            }, 1000);
        });
    }

    public void startPrint() {
        if (printerTextView == null || intentBean == null) {
            return;
        }
        printerTextView.setPrintText(intentBean.getContent()
                , intentBean.getSpeedTime()
                , "|");
        printerTextView.startPrint();
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    private void setTextStyle(TextView textView, int style) {
        AssetManager mgr = getAssets();//得到AssetManager
        String textFont = "fonts/fangzheng_ying_xingkai.TTF";
        if (style == 1) {
            textFont = "fonts/fangzheng_ying_xingkai.TTF";
        } else if (style == 2) {
            textFont = "fonts/huawen_xingkai.ttf";
        } else if (style == 3) {
            textFont = "fonts/huawen_xinwei.TTF";
        }
        Typeface tf = Typeface.createFromAsset(mgr, textFont);//根据路径得到Typeface
        textView.setTypeface(tf);//设置字体
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
